package com.hqyj.controller;

import com.hqyj.pojo.User;
import com.hqyj.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

@RestController
@RequestMapping("/userReful")
public class UserRefulController {


    @Autowired
    IUserService userService;

    //登录
    @RequestMapping("/login")
    public HashMap<String, Object> login(User user, HttpSession session){

        return userService.login(user,session);
    }
    //管理用户列表
    @RequestMapping("/userList")
    public HashMap<String,Object> userListPage(User user){
        HashMap<String,Object> map = userService.queryAll(user);
        return map;
    }

    @RequestMapping("/updateUser")
    public HashMap<String, Object> updateUser(Model model, User user){
        HashMap<String, Object> map = userService.update(user);
        if(map.get("info").equals("保存成功")){
            model.addAttribute("info","保存成功");
        }else {
            model.addAttribute("info", "保存失败");
        }
        return map;
    }

    @RequestMapping("/addUser")
    public HashMap<String, Object> addUser(Model model, User user){
        HashMap<String, Object> map = userService.addUser(user);
        if(map.get("info").equals("保存成功")){
            model.addAttribute("info","保存成功");
        }else {
            model.addAttribute("info", "保存失败");
        }
        return map;
    }
    @RequestMapping("/delUser")
    public HashMap<String, Object> delUser(Integer id){
        return userService.delUser(id);
    }

    @RequestMapping("/updatePwd")
    public HashMap<String, Object> updatePwd(Model model, String pwd, String sysName){
        HashMap<String, Object> map = userService.updatePwd(pwd,sysName);
        if(map.get("info").equals("修改成功")){
            model.addAttribute("info","修改成功");
        }else {
            model.addAttribute("info", "保存失败");
        }
        return map;
    }
}
